package twoD.Csg.tests;

import junit.framework.Assert;

import org.junit.Test;

import twoD.Csg.Circle;
import twoD.Csg.CsgNode;
import twoD.Csg.IPrimitive;
import twoD.Csg.Rectangle;
import twoD.Csg.CsgNode.Operation;

public class CsgNodeTest {

	@Test
	public void testCheck() {
		IPrimitive R = new Rectangle(-1, -1, 2, 2);
		IPrimitive C = new Circle(1, 0, 1);

		CsgNode n1 = new CsgNode();
		n1.setPrimitive(R);
		CsgNode n2 = new CsgNode();
		n2.setPrimitive(C);
		CsgNode n = new CsgNode();

		/*
		 * test union
		 */
		n.setOperation(Operation.UNION, n1, n2);

		double[] p1 = new double[] { -0.9, 0 };
		double[] p2 = new double[] { 1.9, 0 };
		double[] p3 = new double[] { 0.9, 0.8 };
		double[] p4 = new double[] { 1.1, 1 };

		Assert.assertEquals(true, n.contain(p1));
		Assert.assertEquals(true, n.contain(p2));
		Assert.assertEquals(true, n.contain(p3));
		Assert.assertEquals(false, n.contain(p4));

		/*
		 * test intersection
		 */
		n.setOperation(Operation.INTERSECTION, n1, n2);
		Assert.assertEquals(false, n.contain(p1));
		Assert.assertEquals(false, n.contain(p2));
		Assert.assertEquals(true, n.contain(p3));
		Assert.assertEquals(false, n.contain(p4));

		/*
		 * test difference
		 */
		n.setOperation(Operation.DIFFERENCE, n1, n2);
		Assert.assertEquals(true, n.contain(p1));
		Assert.assertEquals(false, n.contain(p2));
		Assert.assertEquals(false, n.contain(p3));
		Assert.assertEquals(false, n.contain(p4));

		n.setOperation(Operation.DIFFERENCE, n2, n1);
		Assert.assertEquals(false, n.contain(p1));
		Assert.assertEquals(true, n.contain(p2));
		Assert.assertEquals(false, n.contain(p3));
		Assert.assertEquals(false, n.contain(p4));

		/*
		 * combine with another circle and test
		 */
		n.setOperation(Operation.UNION, n1, n2);
		IPrimitive C1 = new Circle(-1, 0, 1);
		CsgNode n3 = new CsgNode();
		n3.setPrimitive(C1);

		CsgNode nr = new CsgNode();
		nr.setOperation(Operation.UNION, n, n3);
		Assert.assertEquals(true, nr.contain(p1));
		Assert.assertEquals(true, nr.contain(p2));
		Assert.assertEquals(true, nr.contain(p3));
		Assert.assertEquals(false, nr.contain(p4));

		double[] p5 = new double[] { -1.9, 0 };
		double[] p6 = new double[] { -1.1, 1 };
		Assert.assertEquals(true, nr.contain(p5));
		Assert.assertEquals(false, nr.contain(p6));
	}

}
